package com.sskj.mine.asset;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.quickmark.QuickMarkActivity;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/04
 */
@Route(path = RConfig.MINE_ADD_ADDRESS)
public class AddAddressActivity extends BaseActivity<AddAddressPresenter> {
    @BindView(R2.id.address_edt)
    EditText addressEdt;
    @BindView(R2.id.scan_img)
    ImageView scanImg;
    @BindView(R2.id.remark_edt)
    EditText remarkEdt;
    @BindView(R2.id.submit_btn)
    Button submitBtn;
    @Autowired
     String type;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_add_address;
    }

    @Override
    public AddAddressPresenter getPresenter() {
        return new AddAddressPresenter();
    }

    @Override
    public void initView() {

        ARouter.getInstance().inject(this);
        setTitle(App.INSTANCE.getString(R.string.mineaddAddressActivity1));
        ClickUtil.click(submitBtn, () -> {
            if (checkParam()) {
                mPresenter.addAddress(addressEdt.getText().toString(),
                        remarkEdt.getText().toString(), type);
            }
        });
        ClickUtil.click(scanImg, () -> {
            startActivityForResult(new Intent(mActivity, QuickMarkActivity.class), Constans.SACN_QR);
        });
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(addressEdt.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mineaddAddressActivity2));
            return false;
        }
        return true;
    }

    @Override
    public void initData() {

    }

    public static void start(Context context, String type) {
        Intent intent = new Intent(context, AddAddressActivity.class);
        intent.putExtra(Constans.TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Constans.SACN_QR) {
            String scan_result = data.getStringExtra("scan_result");
            addressEdt.setText(TextUtils.isEmpty(scan_result) ? "" : scan_result);
        }
    }

    public void addSuccess() {
        finish();
    }
}
